#include "server/model/friendmodel.hpp"
#include "db.h"
#include "public.hpp"

bool FriendModel::insert(int userid, int friendid) {
    char sql[SQLMAXSIZE] = {0};
    sprintf(sql, "INSERT INTO friend values(%d,%d)", userid, friendid);
    MySQL mySql;
    return mySql.connect() && mySql.update(sql);
}

vector<User> FriendModel::query(int userid) {
    vector<User> vec;

    char sql[SQLMAXSIZE] = {0};
    sprintf(sql, "SELECT a.id,a.name,a.state FROM user a INNER JOIN friend b on b.friendid = a.id WHERE b.userid=%d", userid);

    MySQL mysql;
    if(mysql.connect()) {
        MYSQL_RES *res = mysql.query(sql);
        if(res != nullptr) {
            //把userid用户的所有离线消息放入vec中返回
            MYSQL_ROW row;
            while((row = mysql_fetch_row(res)) != nullptr) {
                vec.push_back(User(atoi(row[0]), row[1], row[2]));
            }
        }
        mysql_free_result(res);
    }
    return vec;
}
